001    /*
002     * Copyright 2004-2006 Stephen J. McConnell.
003     *
004     * Licensed  under the  Apache License,  Version 2.0  (the "License");
005     * you may not use  this file  except in  compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed  under the  License is distributed on an "AS IS" BASIS,
012     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
013     * implied.
014     *
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.metro.info;
020    
021    import net.dpml.lang.Enum;
022    
023    /**
024     * Lifestyle policy enumeration.
025     *
026     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
027     * @version 1.0.1
028     */
029    public final class LifestylePolicy extends Enum
030    {
031        static final long serialVersionUID = 1L;
032    
033       /**
034        * Transient lifestyle policy.
035        */
036        public static final LifestylePolicy TRANSIENT = new LifestylePolicy( "transient" );
037    
038       /**
039        * Per-thread lifestyle policy.
040        */
041        public static final LifestylePolicy THREAD = new LifestylePolicy( "thread" );
042    
043       /**
044        * Singleton lifestyle policy.
045        */
046        public static final LifestylePolicy SINGLETON = new LifestylePolicy( "singleton" );
047    
048       /**
049        * Singleton lifestyle policy.
050        */
051        public static final LifestylePolicy SYSTEM = new LifestylePolicy( "system" );
052    
053       /**
054        * Array of static activation policy enumeration values.
055        */
056        private static final LifestylePolicy[] ENUM_VALUES = 
057          new LifestylePolicy[]{TRANSIENT, THREAD, SINGLETON, SYSTEM};
058    
059       /**
060        * Returns an array of activation enum values.
061        * @return the activation policies array
062        */
063        public static LifestylePolicy[] values()
064        {
065            return ENUM_VALUES;
066        }
067        
068       /**
069        * Internal constructor.
070        * @param label the enumeration label.
071        */
072        private LifestylePolicy( String label )
073        {
074            super( label );
075        }
076    
077       /**
078        * Return a string representation of the lifestyle constant.
079        * @return the string value
080        */
081        public String toString()
082        {
083            return getName().toUpperCase();
084        }
085        
086       /**
087        * Parse the supplied value into a lifestyle policy.
088        * @param value the value to parse
089        * @return the lifestyle policy
090        * @exception IllegalArgumentException if the value cannot be mapped
091        *   to a lifestyle policy enumeration name
092        */
093        public static LifestylePolicy parse( String value ) throws IllegalArgumentException
094        {
095            if( value.equalsIgnoreCase( "transient" ) )
096            {
097                return TRANSIENT;
098            }
099            else if( value.equalsIgnoreCase( "thread" ) )
100            {
101                return THREAD;
102            }
103            else if( value.equalsIgnoreCase( "singleton" ) )
104            {
105                return SINGLETON;
106            }
107            else if( value.equalsIgnoreCase( "system" ) )
108            {
109                return SYSTEM;
110            }
111            else
112            {
113                final String error =
114                  "Unrecognized lifestyle policy argument [" + value + "]";
115                throw new IllegalArgumentException( error );
116            }
117        }
118    }
119